<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>管理设置</el-breadcrumb-item>
          <el-breadcrumb-item active>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-button
        type="success"
        style="margin-top: 30px"
        @click="addDialogVisible = true"
        >添加权限</el-button
      >
      <el-table
        :data="datalist"
        height="500"
        row-key="pid"
        border
        style="width: 100%; margin-top: 30px; height: 100%"
      >
        <el-table-column prop="pid" label="权限编号" width="180">
        </el-table-column>
        <el-table-column prop="power" label="权限名称" width="180">
        </el-table-column>
        <el-table-column prop="pname" label="权限备注"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="editDialog(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteDialog(scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加按钮 -->
    <el-dialog
      title="添加权限"
      :visible.sync="addDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="权限名称" prop="pass">
          <el-input v-model="form.power" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限备注" prop="pass">
          <el-input v-model="form.pname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="addPoWer">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改按钮 -->
    <el-dialog
      title="修改权限"
      :visible.sync="editDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="权限名称" prop="pass">
          <el-input v-model="form.power" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限备注" prop="pass">
          <el-input v-model="form.pname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="editPoWerclick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addpower, editpower,delpower } from "../../http/api";
import{ mapActions, mapGetters } from 'vuex'
export default {
  data() {
    return {
      // datalist: [],
      //   添加提示框
      addDialogVisible: false,
      editDialogVisible: false,
      form: {
        pid: "",
        power: "",
        pname: "",
      },
    };
  },
  created() {
    this.getpowerlist();
  },
  methods: {
    //获取权限列表
    // getpowerlist() {
    //   let that = this;
    //   powerlist().then((res) => {
    //     if (res.status == 201) {
    //       that.datalist = res.data;
    //     } else {
    //       that.$message.error(res.message);
    //     }
    //   });
    // },
    ...mapActions({
      "getpowerlist":"power/getpowerlistActions"
    }),
    //添加权限
    addPoWer() {
      let that = this;
      addpower(this.form).then((res) => {
        if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getpowerlist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    //点击修改权限按钮
    editDialog(data) {
      console.log(data);
      this.form = {
        pid: data.pid,
        power: data.power,
        pname: data.pname,
      };
      this.editDialogVisible = true;
    },
    //修改权限
    editPoWerclick() {
      let that = this;
      editpower(this.form).then((res) => {
        if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getpowerlist();
        } else {
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    deleteDialog(data) {
        let that = this;
      this.$confirm("此操作将永久删除该权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            delpower(data).then(res=>{
                if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getpowerlist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
            })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //清空数据
    clearDialog() {
      this.editDialogVisible = false;
      this.addDialogVisible = false;
      this.form = {
        pid: "",
        power: "",
        pname: "",
      };
    },
  },
  computed:{
    ...mapGetters({
      "datalist":"power/datalist"
    })
  }
};
</script>

<style>
</style>